package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加菜品或套餐到购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //获取菜品id或套餐id
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();

        ShoppingCart shoppingCart=new ShoppingCart();

        //设置当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断当前添加的是菜品还是套餐
        if(dishId != null){
                DishVO dish = dishMapper.findDishById(dishId);
                BeanUtils.copyProperties(dish,shoppingCart);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setDishId(dishId);
        }else{
            SetmealVO setmealVO = setmealMapper.findById(setmealId);
            BeanUtils.copyProperties(setmealVO,shoppingCart);
            shoppingCart.setAmount(setmealVO.getPrice());
            shoppingCart.setSetmealId(setmealId);
        }

        //查询判断购物车是否已经有相同菜品或套餐(有的话只加数量)
        ShoppingCart cart = shoppingCartMapper.findById(shoppingCart);
        if(cart != null){
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }else{
            shoppingCartMapper.add(shoppingCart);
        }

    }

    /**
     * 查询购物车中的所有商品
     * @return
     */
    @Override
    public List<ShoppingCart> find() {
        //设置当前用户id
        Long userId=BaseContext.getCurrentId();

        List<ShoppingCart> shoppingCarts= shoppingCartMapper.find(userId);
        return shoppingCarts;
    }

    /**
     * 删除购物车中一份商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //设置当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        ShoppingCart cart = shoppingCartMapper.findById(shoppingCart);
        if(cart.getNumber() == 1){
            shoppingCartMapper.delete(shoppingCart);
        }else{
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {

        //设置当前用户id
        Long userId=BaseContext.getCurrentId();

        shoppingCartMapper.clean(userId);
    }
}
